package com.dmgis.qxfw.dataservice.businessservice.yyrb.controller;


import com.dmgis.entity.DmMapPoint;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.important_weather;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.HourFcst1;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.ImportantWeather;
import com.dmgis.qxfw.dataservice.businessservice.common.util.DateTime;
import com.dmgis.qxfw.dataservice.businessservice.yyrb.service.ICityFcstService;
import com.dmgis.qxfw.dataservice.gisservice.service.ITransformService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/xtrb")
public class CityFcstController {

	@Resource
	private ICityFcstService cityfcst;

	@Resource
	private ITransformService transformService;


	/**
	 *  初始化08时预报最新时间
	 * @return
	 */
	@RequestMapping("/Init08TempLoadTime")
	@ResponseBody
	public String Init08TempLoadTime() {
		String newestDate = cityfcst.Init08TempLoadTime();
		return newestDate;
	}

	/**
	 * 初始化08时文档最新时间
	 * @return
	 */
	@RequestMapping("/InitHourForecast1Time")
	@ResponseBody
	public String InitHourForecast1Time() {
		String newestDate = cityfcst.InitHourForecast1Time();
		return newestDate;
	}

	/**
	 * 08时天气预报产品
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/hourForecast1")
	@ResponseBody
	public List<HourFcst1> hourForecast1(HttpServletRequest request, Model model) {
		String strStartTime = request.getParameter("StartTime") + " 00:00"; // 开始时间
		String strEndTime = request.getParameter("EndTime") + " 23:59"; // 结束时间
		List<HourFcst1> resultList = cityfcst.hourForecast1(strStartTime, strEndTime);
		return resultList;
	}

	/**
	 * 初始化界面
	 * @return
	 */
	@RequestMapping("/initCZPage")
	@ResponseBody
	public Object initChengZhenPage() {
		ArrayList<Object> arrayList = new ArrayList<Object>();/* object各种类型数据 */
		important_weather newestDate = cityfcst.getIWNewDate();
		// 加载列表
		DateTime date = new DateTime(newestDate.getObservetime());// DateTime类型
		String startDate = date.AddHours(-1).toDateTimeString();// String类型
		String endDate = date.toDateTimeString();// String类型
		arrayList.add(startDate);
		arrayList.add(endDate);
		return arrayList;
	}

	/**
	 * 重要天气
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/ChenZhenYuBao")
	@ResponseBody
	public Object getCityFcst(HttpServletRequest request, Model model) throws Exception {
		String startDate = request.getParameter("startDate");// 开始时间
		String endDate = request.getParameter("endDate");// 结束时间
		List<ImportantWeather> result = new ArrayList<ImportantWeather>();
		List<important_weather> resultList = cityfcst.getimportantweather(startDate,endDate);
		if(resultList.size()>0 && resultList!=null){
			for(important_weather wea : resultList){
				ImportantWeather impor = new ImportantWeather();
				impor.setStationId(wea.getStationid());
				impor.setStationName(wea.getStationname());
				impor.setObservTime(new DateTime(wea.getObservetime()).toDateTimeString());
				impor.setWeathertype(wea.getWeathertype());
				impor.setWeatherdetail(wea.getWeatherdetail());
				impor.setLon(wea.getLon());
				impor.setLat(wea.getLat());
				DmMapPoint dmDot = new DmMapPoint(wea.getLon(), wea.getLat());
				DmMapPoint ltDot = transformService.LonLatConvertToLambert(dmDot.x, dmDot.y);
				impor.setX(ltDot.x);
				impor.setY(ltDot.y);
				result.add(impor);
			}
		}
		return result;
	}
}
